package de.bazinga.rotw.vehicle;

import java.io.File;
import java.net.MalformedURLException;
import java.net.URL;

import com.jme.bounding.BoundingBox;
import com.jme.math.FastMath;
import com.jme.math.Vector3f;
import com.jme.scene.Node;
import com.jme.scene.Spatial;
import com.jme.util.CloneImportExport;
import com.jmex.jbullet.PhysicsSpace;
import com.jmex.jbullet.collision.shapes.BoxCollisionShape;
import com.jmex.jbullet.nodes.PhysicsVehicleNode;

import de.bazinga.rotw.model.ModelCache;

public class CabrioCar extends Car {
	private static final long serialVersionUID = 1L;

	
	public CabrioCar(Vector3f startLocation) {
		super(startLocation);
		
		setAcceleration(150f);
		setTurnSpeed(0.8f);
		setBrakeSpeed(10);
	}
	
	
	protected void loadModel() {
		URL jeepUrl = null;
		try {
			jeepUrl = new File("vehicles/cabrio/").toURI().toURL();
		} catch (MalformedURLException e1) {
			e1.printStackTrace();
		}
		Node car = ModelCache.getInstance().loadModel(jeepUrl, jeepUrl, "/vehicles/cabrio/audi.3ds");

		Node body = new Node();
    	body.attachChild(car.getChild("body"));
    	
    	body.setModelBound(new BoundingBox());
    	body.updateModelBound();
		
    	body.setLocalScale(5f);
        body.setLocalTranslation(new Vector3f(0,-1,0));
        body.updateRenderState();
        this.attachChild(body);
        body.updateGeometricState(0, false);
		//this.attachChild(ant);
		

        
		Node wheelNode = ModelCache.getInstance().loadModel(jeepUrl, jeepUrl, "/vehicles/cabrio/wheel.3ds");
		
        wheelNode.setLocalScale(5f);
        wheelNode.setLocalTranslation(Vector3f.ZERO);
        wheelNode.updateRenderState();
        
        CloneImportExport wheelClone = new CloneImportExport();
        wheelClone.saveClone(wheelNode);
        
        // Create four wheels and add them at their locations
        Spatial wheel1 = (Spatial)wheelClone.loadClone();
        Spatial wheel2 = (Spatial)wheelClone.loadClone();
        Spatial wheel3 = (Spatial)wheelClone.loadClone();
        Spatial wheel4 = (Spatial)wheelClone.loadClone();


        wheel1.setModelBound(new BoundingBox());
        wheel1.updateModelBound();
        wheel2.setModelBound(new BoundingBox());
        wheel2.updateModelBound();
        wheel3.setModelBound(new BoundingBox());
        wheel3.updateModelBound();
        wheel4.setModelBound(new BoundingBox());
        wheel4.updateModelBound();
        
        
        // Add a physics vehicle to the world
        BoxCollisionShape boxCollisionShape=new BoxCollisionShape((BoundingBox)body.getWorldBound());
        
        physicsCar=new PhysicsVehicleNode(body,boxCollisionShape,130);
        physicsCar.setFrictionSlip(0.8f);
        
        physicsCar.setMaxSuspensionTravelCm(40);
        
        float stiffness=50;//200=f1 car
        float compValue=0.1f; //(lower than damp!)
        float dampValue=0.3f;
        physicsCar.setSuspensionCompression(compValue*2.0f*FastMath.sqrt(stiffness));
        physicsCar.setSuspensionDamping(dampValue*2.0f*FastMath.sqrt(stiffness));
        physicsCar.setSuspensionStiffness(stiffness);
         
        
        
        Vector3f wheelDirection=new Vector3f(0,-1,0);
        Vector3f wheelAxle=new Vector3f(-1,0,0);
        
        // Front left
        physicsCar.addWheel(wheel1, new Vector3f(0.95f,-0.4f /* 0.4f */,1.6f), wheelDirection, wheelAxle, 0.2f, 0.34f, true);
        physicsCar.setRollInfluence(0, 1);

        // Front right
        physicsCar.addWheel(wheel2, new Vector3f(-0.95f,-0.4f,1.6f), wheelDirection, wheelAxle, 0.2f, 0.34f, true);
        physicsCar.setRollInfluence(1, 1);

        physicsCar.addWheel(wheel3, new Vector3f(0.95f,-0.4f,-1.83f), wheelDirection, wheelAxle, 0.2f, 0.34f, false);
        physicsCar.setRollInfluence(2, 1);

        physicsCar.addWheel(wheel4, new Vector3f(-0.95f,-0.4f,-1.83f), wheelDirection, wheelAxle, 0.2f, 0.34f, false);
        physicsCar.setRollInfluence(3, 1);

        
		//physicsCar.setLocalTranslation(new Vector3f(-20,5,0));
		physicsCar.setLocalTranslation(new Vector3f(0,3,0));
		
		this.attachChild(physicsCar);
        physicsCar.updateRenderState();
		
		
		PhysicsSpace.getPhysicsSpace().add(physicsCar);
	}
	
	
	public Vector3f getCameraLocation() {
		return new Vector3f(
				getLocalTranslation().x,
				getLocalTranslation().y + 1.5f,
				getLocalTranslation().z);
	}
	
	
	public void setLocalTranslation(Vector3f trans) {
		physicsCar.setLocalTranslation(trans.x, trans.y, trans.z);
	}
	
	public Vector3f getLocalTranslation() {
		return physicsCar.getLocalTranslation();
	}
}